﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using H2.NET.Common;
using H2.NET.Web.Workflow.DwzCode;
using H2.NET.Web.Workflow.Helper;
using Ultimus.OC;
using Ultimus.WFServer;
using UServLib;
using H2.NET.Business.Core;
using H2.NET.Data.Common;
using System.Text;
using System.Data.SqlClient;
using H2.NET.DBUtility;
using log4net;
using System.Reflection;

namespace H2.NET.Web.Workflow.Workflow
{
    public partial class ChangeIncidentStatus : BasePage
    {
        /// <summary>
        /// 日志记录
        /// </summary>
        private static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public List<Task> CurrentSteps = new List<Task>();

        protected void Page_Load(object sender, EventArgs e)
        {
            var action = AksRequest.GetQueryStringValue("action");
            if (string.IsNullOrEmpty(action)) //默认页面加载
            {
                var processName = AksRequest.GetString("process").Trim();
                var incidentNo = AksRequest.GetString("incident");

                hidProcess.Value = processName;
                hidIncident.Value = incidentNo;

            }
            else if (action == "save") //form提交
            {
                var flag = false;
                var status = AksRequest.GetInt("status", 0);

                var processNames = AksRequest.GetString("hidProcess");
                var aryProcessNames = string.IsNullOrEmpty(processNames) ? new string[0] : processNames.Split(',');
                var incidentNoes = AksRequest.GetString("hidIncident");
                var aryIncidentNoes = string.IsNullOrEmpty(incidentNoes) ? new string[0] : incidentNoes.Split(',');

                var process = string.Empty;
                var success = 0;
                var fail = 0;
                for (var i = 0; i < aryProcessNames.Length; i++)
                {
                    //加载实例信息
                    var incident = new Incident();
                    var incidentNo = 0;
                    int.TryParse(aryIncidentNoes[i], out incidentNo);
                    var processName = string.IsNullOrEmpty(aryProcessNames[i]) ? "" : aryProcessNames[i].Trim();

                    process += processName + "(" + incidentNo + ")" + ";";

                    try
                    {
                        var strSql = new StringBuilder();
                        strSql.Append("UPDATE INCIDENTS SET STATUS=@Status, ENDTIME=@EndTime ");
                        strSql.Append("WHERE PROCESSNAME=@Process AND INCIDENT=@Incident");
                        SqlParameter[] parameters = {
					                new SqlParameter("@Process", SqlDbType.VarChar,50),
					                new SqlParameter("@Incident", SqlDbType.Int,4),
					                new SqlParameter("@Status", SqlDbType.Int,4),
                                    new SqlParameter("@EndTime", SqlDbType.DateTime)
                            };
                        parameters[0].Value = processName;
                        parameters[1].Value = incidentNo;
                        parameters[2].Value = status;
                        if (status == 2 || status == 4)
                        {
                            parameters[3].Value = DateTime.Now;
                        }
                        else
                        {
                            parameters[3].Value = null;
                        }                         

                        DbHelperSQL.connectionString = PubConstant.GetConnectionString("bpmDB");
                        int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
                        flag = rows > 0;

                        if (flag)
                        {
                            success++;
                        }
                        else
                        {
                            fail++;
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.Error(ex.Message);
                        fail++;
                    }
                }

                var incidentManageLog = new t_log_IncidentManage
                {
                    Process = process,
                    Incident = 0,
                    Status = flag,
                    Action = "流程实力状态管理",
                    Description = "成功：" + success + " 失败：" + fail,
                    Creator = UserAPI.UserFullName,
                    CreateOn = DateTime.Now
                };
                Storage.Save(incidentManageLog);

                if (flag)
                {
                    SuccessMsg();
                }
                else
                {
                    ErrorMsg("操作失败");
                }
            }
        }


    }
}